package com.sun.jdk.date.jdk8after;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 计算任意时间与下一个圣诞节/儿童节/劳动节各相差多少天
 */
public class TemporalQueryPractise2WithLambda {

    public static Map<String,Long> getDays(LocalDate localDate) {

        Map<String,Long> days = localDate.query(temporal -> {
            LocalDate now = LocalDate.from(temporal);

            // 封装当年的圣诞节
            LocalDate christmasDay = LocalDate.of(now.getYear(), Month.DECEMBER, 25);
            // 封装当年的儿童节时间
            LocalDate childDay = LocalDate.of(now.getYear(), Month.JUNE, 1);
            // 封装当年的劳动节时间
            LocalDate laborDay = LocalDate.of(now.getYear(), Month.MAY, 1);


            // 判断当前四化建是否超过上劳动节
            if (now.isAfter(christmasDay)) christmasDay = christmasDay.plusYears(1);
            if (now.isAfter(childDay)) childDay = childDay.plusYears(1);
            if (now.isAfter(laborDay)) laborDay = laborDay.plusYears(1);

            // 计算距离下一个日期的天数
            Map<String,Long> result = new HashMap<>();
            result.put("圣诞节", ChronoUnit.DAYS.between(now, christmasDay));
            result.put("儿童节", ChronoUnit.DAYS.between(now, childDay));
            result.put("劳动节", ChronoUnit.DAYS.between(now, laborDay));
            return result;
        });
        return days;
    }

    public static void main(String[] args) {
        Map<String,Long> days = getDays(LocalDate.now());
        System.out.println("days = " + days);
    }
}
